/**
 * Created by huangying on 16/10/23.
 */
window.onload = function () {
    var url = location.search; //获取url中"?"符后的字串
    mototype_id = url.substring(13, url.length);
    var _name = "";

    //根据url后缀,确定当前所显示的摩托车类型的名字
    $.get(url_link + 'mototypes', function (data) {
        var obj = JSON.parse(data);
        if (obj.code == 0) {
            $.each(obj.data, function (i, item) {
                if (mototype_id == item.mototype_id) {
                    _name = item.name;
                    $('.crumbs_content').text(_name);
                    $('.theme_banner img').attr('src', item.cover);
                    $('.theme_title h2').html(_name);
                    $('.theme_title h2').attr('data-name', _name);
                }
            })
        }
    });


    data_motocycle = {
        page: 1,
        rows: 20,
        mototype_id: mototype_id
    };
    discover_theme_content();

    var _pagenum = Math.ceil(total / data_motocycle.rows);
    if (_pagenum > 1) {
        //需要分页展示
        $('.pageNum').show();
        $(".pageNum").createPage({
            pageCount: _pagenum,
            current: data_motocycle.page,
            backFn: function (p) {
                //单击回调方法，p是当前页码
                data_motocycle.page = p;
                discover_theme_content();
            }
        });
    }

    //登陆后才能判断有木有关注该车型
    if (user_id) {
        //判断该车型是否已被关注
        $.get(url_link + 'mototypelikes', {user_id: user_id}, function (data) {
            var obj = JSON.parse(data);
            var _num = 0;
            if (obj.code == 0) {
                $.each(obj.data, function (i, item) {
                    if (mototype_id == item.mototype_id) {
                        _num = 1;
                    }
                });
                if (_num == 0) {
                    $('.theme_title h2').html(_name + "<p class='interest-follow-btn'> [ 加关注 ] </p>");
                } else {
                    $('.theme_title h2').html(_name + "<p class='interest-unfollow-btn'> [ 取消关注 ] </p>");
                }
            }
        });
    }
};

//关注
$(document).on('click', '.theme_title h2 p', function () {
    var m = $(this);
    if (m.hasClass('interest-follow-btn')) {
        //加关注
        layer.msg('您是否要对此类发现进行关注？', {
            time: 0, //不自动关闭
            btn: ['确定', '取消'],
            yes: function (index) {
                layer.close(index);
                $.post(url_link + 'mototypelikes', {
                    user_id: user_id,
                    mototype_id: mototype_id
                }, function (data) {
                    try {
                        layer.msg("关注成功!", {time: 1000});
                        setTimeout(function () {
                            location.reload();
                        }, 1000);
                    } catch (e) {

                    }
                })

            }
        });
    } else {
        //取消关注
        layer.msg('您是否要取消关注此类发现？', {
            time: 0, //不自动关闭
            btn: ['确定', '取消'],
            yes: function (index) {
                layer.close(index);
                $.post(url_link + 'mototypelikes', {
                    user_id: user_id,
                    mototype_id: mototype_id
                }, function (data) {
                    try {
                        layer.msg("取消关注成功!", {time: 1000});
                        setTimeout(function () {
                            location.reload();
                        }, 1000);
                    } catch (e) {

                    }
                })

            }
        });
    }

});

//帖子质量,作者性别
$(document).on('click', '.screening_overflow li', function () {
    page = 1;
    sex = data_motocycle.sex;
    is_nice = data_motocycle.is_nice;
    $('.interest-list').html('');
    var _name = $(this).children('a').text();
    if (_name == "全部") {
        $('.thread_title').html("帖子质量");
    } else if (_name == "精华" || _name == "普通") {
        $('.thread_title').html(_name);
    } else if (_name == "不限") {
        $('.sex_title').html("作者性别")
    } else {
        $('.sex_title').html(_name)
    }
    if (is_nice == undefined && sex == undefined) {
        if (_name == "全部" || _name == "不限") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id
            };
        } else if (_name == "精华") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: 1
            };
        } else if (_name == "普通") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: 0
            };
        } else if (_name == "男") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                sex: 1
            };
        } else if (_name == "女") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                sex: 0
            };
        }
    }
    if (is_nice != undefined && sex == undefined) {
        if (_name == "全部") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id
            };
        } else if (_name == "精华") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: 1
            };
        } else if (_name == "普通") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: 0
            };
        } else if (_name == "不限") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: is_nice
            };
        } else if (_name == "男") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: is_nice,
                sex: 1
            };
        } else if (_name == "女") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: is_nice,
                sex: 0
            };
        }
    }
    if (is_nice == undefined && sex != undefined) {
        if (_name == "全部") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                sex: sex
            };
        } else if (_name == "精华") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: 1,
                sex: sex
            };
        } else if (_name == "普通") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: 0,
                sex: sex
            };
        } else if (_name == "不限") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id
            };
        } else if (_name == "男") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                sex: 1
            };
        } else if (_name == "女") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                sex: 0
            };
        }
    }
    if (is_nice != undefined && sex != undefined) {
        if (_name == "全部") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                sex: sex
            };
        } else if (_name == "精华") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: 1,
                sex: sex
            };
        } else if (_name == "普通") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: 0,
                sex: sex
            };
        } else if (_name == "不限") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: is_nice
            };
        } else if (_name == "男") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: is_nice,
                sex: 1
            };
        } else if (_name == "女") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: is_nice,
                sex: 0

            };
        }
    }
    $('.interest-list').html('')
    $('.order-area a').removeClass('active');
    $('.order-area a').addClass('normal');
    $('.order-area a:first').addClass('active');
    $('.order-area a:first').removeClass('normal');
    discover_theme_content();
});

//评论最多,点击最高,最新发布
$(document).on('click', '.order-area a', function () {
    page = 1;
    sex = data_motocycle.sex;
    is_nice = data_motocycle.is_nice;
    $('.interest-list').html('')
    $('.order-area a').removeClass('active');
    $('.order-area a').addClass('normal');
    $(this).removeClass('normal');
    $(this).addClass('active');
    var _text = $(this).text();
    if (sex == undefined && is_nice == undefined) {
        if (_text == "评论最多") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                mostComment: 1
            }
        } else if (_text == "点击最高") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                mostSee: 1
            }
        } else if (_text == "最新发布") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id
            }
        }
    }
    if (sex == undefined && is_nice != undefined) {
        if (_text == "评论最多") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: is_nice,
                mostComment: 1
            }
        } else if (_text == "点击最高") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: is_nice,
                mostSee: 1

            }
        } else if (_text == "最新发布") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: is_nice
            }
        }
    }
    if (sex != undefined && is_nice == undefined) {
        if (_text == "评论最多") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                sex: sex,
                mostComment: 1

            }
        } else if (_text == "点击最高") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                sex: sex,
                mostSee: 1
            }
        } else if (_text == "最新发布") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                sex: sex
            }
        }
    }
    if (sex != undefined && is_nice != undefined) {
        if (_text == "评论最多") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: is_nice,
                sex: sex,
                mostComment: 1
            }
        } else if (_text == "点击最高") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: is_nice,
                sex: sex,
                mostSee: 1
            }
        } else if (_text == "最新发布") {
            data_motocycle = {
                page: page,
                rows: 20,
                mototype_id: mototype_id,
                is_nice: is_nice,
                sex: sex
            }
        }
    }
    discover_theme_content();
});

//文章列表展示
function discover_theme_content() {
    $.ajax({
        type: 'get',
        url: url_link + 'articles',
        async: false,
        data: data_motocycle,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var discover_theme_info = "";
                total = obj.data.total;
                if (obj.code == 0) {
                    if (obj.data.total != 0) {
                        $.each(obj.data.rows, function (i, item) {
                            discover_theme_info += "<li data-article=" + item.article_id + "><div class='img-box'>" +
                                "<a href='publish/show.html?article_id=" + item.article_id + "' target='_blank'>";
                            if (item.cover) {
                                discover_theme_info += "<img src='" + item.cover + "' style='width: 252px;height: 168px'>";
                            } else {
                                var _content = item.content.split("$$##MotorTravel##$$");
                                var _count = 0;
                                for (var _a = 0; _a < _content.length; _a++) {
                                    var m = _content[_a].substring(0, 13);
                                    if (m == "$$##Image##$$") {
                                        //有图
                                        _count = 1;
                                        var _content_image_description = _content[_a].split("$$##ImageDescription##$$");
                                        var _content_image_url = _content_image_description[0].split("$$##ImageInfo##$$");
                                        discover_theme_info += "<img src='" + _content_image_url[0].substring(13, _content_image_url[0].length) + "' style='width: 256px;height: 168px;'>"
                                        break;
                                    }
                                }
                                //纯文字,没有图的情况下,使用备用图片
                                if (_count == 0) {
                                    discover_theme_info += "<img src='../image/base/none_img.png' style='width: 252px;height: 168px;'>";
                                }
                            }

                            discover_theme_info += "</a>" +
                                "<div class='about-count'>" +
                                "<span class='click'><i></i>" + item.see_count + "</span>" +
                                "<span class='comment'><i></i>" + item.comment_count + "</span>" +
                                "<span class='praise'><i></i>" + item.gift_count + "</span>" +
                                "</div>" +
                                "</div>" +
                                "<h4>" +
                                "<a href='publish/show.html?article_id=" + item.article_id + "' target='_blank' style='line-height:14px'>";
                            if (item.is_nice == 1) {
                                discover_theme_info += "<i class='essence'></i>"
                            }
                            discover_theme_info += item.title + "</a>" +
                                "</h4>" +
                                "<div class='user-info'>" +
                                "<a href='lookat/article.html?user_id=" + item.user_id + "' target='_blank' class='user'><i>"
                            if (item.user.avatar) {
                                discover_theme_info += "<img src=" + item.user.avatar + " style='width: 25px;height: 25px;'>"
                            } else {
                                discover_theme_info += "<img src='../image/base/none.png' style='width: 25px;height: 25px;'>"
                            }

                            discover_theme_info += "</i><em>" + item.user.nickname + "</em></a>";
                            //判断是否是今日更新
                            var updated_time = item.updatedDate.substring(0, 10);
                            if (mytime == updated_time) {
                                item.updatedDate = "今天   " + item.updatedDate.substring(11, item.updatedDate.length)
                            }
                            discover_theme_info += "<span class='pub-time'>" + item.updatedDate + "</span></div></li>"
                        });
                    } else {
                        if ($('.interest-list li').length == 0) {
                            discover_theme_info += "<h2 class='none_info'>暂时没有更多文章</h2>";
                            $('.pageNum').hide()
                        }
                    }
                    $('.interest-list').html(discover_theme_info)
                }
                page = data_motocycle.page;
            } catch (e) {

            }
        }
    })
}